<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        table {
            width: 500px;
            margin: 100px auto;
            border-collapse: collapse;
            text-align: center;
        }
        
        table,
        td,
        th {
            border: 1px solid #333;
        }
        
        thead tr {
            height: 40px;
            background-color: #ccc;
        }
    </style>
</head>

<body>
    <table cellspacing="0">
        <thead>
            <tr>
                <th>姓名</th>
                <th>科目</th>
                <th>成绩</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
        </tbody>
    </table>
    <script>
        //1.创建数据
        var datas = [{
            name: '王嘉尔',
            subject: 'JS',
            score: 100
        }, {
            name: '朴灿烈',
            subject: 'JS',
            score: 78
        }, {
            name: '鹿晗',
            subject: 'JS',
            score: 99
        }, {
            name: '白敬亭',
            subject: 'JS',
            score: 100
        }];
        //2.在tbody里创建行(有几个人，就创建几行,通过数组长度知道)
        var tbody = document.querySelector("tbody");
        for (var i = 0; i < datas.length; i++) {
            //创建行(创建元素 document.createElement('tagName'))
            var tr = document.createElement("tr");
            tbody.appendChild(tr);
            //在行里面创建单元格(单元格数量取决于每个对象里面的属性的个数)
            for (var k in datas[i]) {
                var td = document.createElement("td");
                //datas[i]表示对象，加上k就是属性值
                td.innerHTML = datas[i][k];
                tr.appendChild(td);

            }
            //创建有删除两个字的单元格
            var td = document.createElement("td");
            td.innerHTML = "<a href='javaacript:;'>删除</a>"
            tr.appendChild(td)
        }
        var as = document.querySelectorAll("a");
        for (var i = 0; i < as.length; i++) {
            as[i].onclick = function() {
                tbody.removeChild(this.parentNode.parentNode)
            }
        }
    </script>
</body>

</html>